<template>
  <div id="addAccount">
    <table>
      <tr>
        <td>上级分类</td>
        <td>
          <el-button @click="choose = true">请选择分类</el-button>
          <div v-show="choose">
            <el-tree
              ref="tree"
              :data="treeData"
              :props="defaultProps"
              node-key="id"
              show-checkbox
              check-strictly
              @check-change="handleCheckChange"
            >
            </el-tree>
          </div>
        </td>
      </tr>
      <br />
      <tr>
        <td>类别</td>
        <td><el-input v-model="categoryname"></el-input></td>
      </tr>
      <br />
      <tr>
        <td></td>
        <td>
          <el-button style="margin-left: 100px" @click="flush()"
            >取 消</el-button
          >
          <el-button style="margin-left: 170px" type="primary" @click="insert()"
            >新增</el-button
          >
        </td>
      </tr>
    </table>
  </div>
</template>
<script>
export default {
  name: "AddEmployee",
  data() {
    return {
      treeData: [],
      pageInfo: "",
      defaultProps: {
        children: "children",
        label: "categoryname",
      },
      choose: false,
      selectedid: 0,
      categoryname: "",
    };
  },
  methods: {
    handleCheckChange(node, checked) {
      // 如果取消勾选，则不进行操作
      if (checked) {
        // 取消勾选其他所有节点
        const allCheckedNodes = this.$refs.tree.getCheckedNodes();
        allCheckedNodes.forEach((checkedNode) => {
          if (checkedNode.id !== node.id) {
            this.$refs.tree.setChecked(checkedNode, false);
          }
        });
        // 设置当前勾选节点的 id
        this.selectedid = node.id;
        console.log(this.selectedid);
      } else {
        // 如果节点被取消勾选，重置 selectedid
        this.selectedid = null;
      }
    },
    // 用于初始化或重置 selectedid
    resetChecked() {
      this.selectedid = null;
      this.$refs.tree.setCheckedKeys([]);
    },
    buildTree(data) {
      const tree = [];
      const map = {};

      // 将所有节点添加到map中，方便后续查找
      data.forEach((item) => {
        map[item.id] = { ...item, children: [] };
      });

      // 构建树形结构
      data.forEach((item) => {
        if (item.parentid === 0) {
          tree.push(map[item.id]);
        } else {
          if (map[item.parentid]) {
            map[item.parentid].children.push(map[item.id]);
          }
        }
      });

      return tree;
    },
    searchPage: function () {
      this.axios({
        url: "/erp/goods/category/getList",
        // params: {
        //     kind: "客户",
        //     status: this.status
        // }
      }).then((rs) => {
        console.log(rs.data);
        this.pageInfo = rs.data.list;
        this.treeData = this.buildTree(this.pageInfo);
        console.log(this.treeData);
      });
    },
  },
  created: function () {
    this.searchPage();
    this.resetChecked();
  },
};
</script>

<style scoped>
#addAccount {
  width: 100%;
  /* height: 250px; */
  /* overflow: scroll; */
  /* background-color: pink; */
}

#contact {
  width: 90%;
  margin: auto;
  border-collapse: collapse;
  color: #707276;
}

#contact th,
#contact td {
  border: 1px #c0c4cc solid;
  height: 35px;
  min-width: 30px;
}

p {
  color: #4b4a4a;
  font-size: 18px;
}

.el-input {
  width: 240px;
}

table td :not([id="contact"]) {
  text-align: left;
  padding-right: 10px;
  /* padding-top: 10px;
    padding-bottom: 10px; */
}

td:nth-child(odd) {
  text-align: center;
  padding-right: 10px;
}
</style>